<template>
  <div class="wrap-container sn-container"> 
    <div class="sn-content"> 
      <div class="sn-title">今日网络流量 -- 共{{sumTraffic>1000?sumTraffic/1000+'/GB':sumTraffic+'/MB'}}</div> 
      <div class="sn-body"> 
        <div class="wrap-container"> 
          <div class="chartsdom" id="chart_networkTraffic"></div>
        </div> 
      </div> 
    </div>   
  </div>
</template>
<script>
import '../../assets/js/echarts5.js'
var myChart;
var data= [];
var xData = [];
var option;
export default {
  name: "networkTraffic",
  data() {
    
    return {
      timer: null,
      tableData: [],
      sumTraffic: ''
    }
  },
  props: {
    time: {
      type: Number,
      default: 60000
    },
  },
  mounted() {
    let that = this;
    myChart = echarts.init(document.getElementById('chart_networkTraffic'))
    option= {
      title: {
        show: false, // 是否显示title
        text: '暂无数据',
        left: 'center',
        top: 'center',
        textStyle: {
          color: '#fff',
          fontSize: 16,
          fontWeight: 400
        },
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        },
        formatter: '{a0}: <br />{b0}: {c0}'+'/MB'
      },
      xAxis: {
        type: 'category',
        data: xData,
        animationDuration: 1000,
        animationDurationUpdate: 1000,
        axisLabel: {
          color: '#999'
        },
        axisTick: {
          show: false
        }, 
        axisLine: {
          show: true,
          lineStyle: {
            color: 'rgba(78, 241, 246, 0.3)',
          }
        },
          
      },
      yAxis: {
        max: 'dataMax',
        axisLabel: {
          color: '#999'
        },
        axisTick: {
          show: false
        }, 
        splitLine: {
          show: false
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: 'rgba(78, 241, 246, 0.3)'
          }
        }
      },
      grid: {
        top: 20,
        left: 20,
        right: 20,
        bottom: 20,
        containLabel: true
      },
      series: [{
        name: '网络流量',
        type: 'bar',
        stack: 'one',
        data: data,
        label: {
            show: true,
            position: 'top',
            valueAnimation: true,
            color: 'rgb(78, 241, 246)'
        },
        itemStyle: {
          normal: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              offset: 1, 
              color: 'rgba(0, 0, 0, 0)'
            },{
              offset: 0.5, 
              color: 'rgba(69, 194, 198,0.3)'
            },{
              offset: 0, 
              color: 'rgb(78, 241, 246)'
            }])
          }
        },
        barWidth: '10px'
      }],
      animationDuration: that.time,
      animationDurationUpdate: that.time,
      animationEasing: 'linear',
      animationEasingUpdate: 'linear'
    }
    if(this.timer){
      clearInterval(this.timer)
    }
    setTimeout(function() {
        that.getEchart()
    }, 0);
    this.timer=setInterval(function () {
        that.getEchart()
    }, that.time);

  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    getEchart() {
      const u = '/reception/bigScreen/querySiteTraffic';
      this.$axios.get(u,{headers:{'cloud-Auth-Token': localStorage.getItem('cloud-Auth-Token')}})
      .then((res)=>{
        if(res.data.code == 200 && res.data.data.siteTraffic.length){
          this.tableData =  res.data.data.siteTraffic;
          this.sumTraffic =  res.data.data.sumTraffic;
          this.success();
        }else{
          this.$message.error('暂无数据');
          this.false();
        };
      }).catch((err)=>{
          console.log(err);
      });
      window.addEventListener('resize', () => {
        myChart.resize();
      });
    },
    success() {
      var xData = [];var yData=[];
      for(let i=0;i<this.tableData.length;i++){
        xData.push(this.tableData[i].name);
        yData.push(this.tableData[i].value);
      }
      option.xAxis.data= xData;
      option.series[0].data = yData ;
      option.title.show = false;
      myChart.setOption(option);
    },
    false() {
      option.xAxis.data= [];
      option.series[0].data = [] ;
      option.title.show = true;
      myChart.setOption(option);
    }
  }
};
</script>

<style lang="less" scoped>
.sn-container {
  .chartsdom {
    width: 100%;
    height: 95%;
  }
}
</style>
